﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Chambills.Present.Core;
using Chambills.Present.Core.NotifyOnChange;
using NUnit.Framework;

namespace Chambills.Present.Validation.Tests
{
    [TestFixture]
    public class AttributeBasedViewModelValidatorContributorFixture
    {


        private AttributeBasedViewModelValidatorContributor validator;
        private TestViewModel model;


        [SetUp]
        public void Setup()
        {
            model = new TestViewModel();
            validator = new AttributeBasedViewModelValidatorContributor { Model = model };
        }

       
        

        [Test]
        public void ValidFieldsProduceNoError()
        {
            model.RequiredField = "test";
            Assert.IsTrue(validator.GetCurrentErrors().IsEmpty(), "Error Message should be empty");
        }

        [Test]
        public void FieldsAreValidated()
        {
            model.RequiredField = null;
            Assert.IsFalse(validator.GetCurrentErrors().IsEmpty());
        }

        [Test]
        public void CorrectMessageReturned()
        {
            model.RequiredField = null;
            Assert.AreEqual(TestViewModel.RequiredMsg, validator.GetCurrentErrors().FirstOrDefault());
        }


        //TODO: Fix this so we can inherit and/or override validations
        [Test(Description = "Too Difficult to work out which validation is right, if you override a property, readd the validation")]
        public void OverridenPropertiesDoNotInheritValidations()
        {
            model = new DerivedTestViewModel();
            validator = new AttributeBasedViewModelValidatorContributor { Model = model };
            model.RequiredField = null;
            Assert.IsEmpty(validator.GetCurrentErrors());
        }

        [Test]
        public void InheritedPropertyisValidated()
        {
            model = new DerivedTestViewModel();
            validator = new AttributeBasedViewModelValidatorContributor { Model = model };
            model.PositiveInt = -10;
            Assert.IsNotEmpty(validator.GetCurrentErrors());
        }
    }

    public class DerivedTestViewModel : TestViewModel
    {
        [System.ComponentModel.DataAnnotations.RegularExpression("/d", ErrorMessage = "Error")]
        public override string RequiredField
        {
            get
            {
                return base.RequiredField;
            }
            set
            {
                base.RequiredField = value;
            }
        }

     
    }

   

    public class TestViewModel : NotifiableObject, IViewModel
    {
        public const string RequiredMsg = "Required Field";

        private string requiredField;

     

        [Required(ErrorMessage = RequiredMsg)]
        public virtual string RequiredField
        {
            get { return requiredField; }
            set
            {
                requiredField = value;
                OnPropertyChanged(()=>RequiredField);
            }
        }

        private int positiveInt;

        [System.ComponentModel.DataAnnotations.Range(0, 100, ErrorMessage = "Not Positive")]
        public int PositiveInt
        {
            get { return positiveInt; }
            set
            {
                positiveInt = value;
                OnPropertyChanged(() => this.PositiveInt);
            }
        }
    }
}
